#include "main.h"

#include <Arduino.h>
#include <lvgl.h>
// UI
#include "my_ui\myGUI.h"
//驱动
#include "dev_port/lv_port_disp.h"
#include "dev_port/lv_port_fs.h"
#include "dev_port/lv_port_indev.h"
#include "lodepng.h"

// #include "examples\lv_examples.h"
#include <lv_demo.h>

void tim1Interrupt() { lv_tick_inc(1); }

#if LV_USE_LOG != 0
/* Serial debugging */
void my_print(const char* buf) {
  Serial.printf("%s\n", buf);
  Serial.flush();
}

#endif

void setup() {
  Serial.begin(115200); /* prepare for possible serial debug */
  Serial.println("Hello Arduino! (V8.0.X)");
  Serial.println("I am LVGL_Arduino");

  // LVGL初始化
  lv_init();

#if LV_USE_LOG != 0
  lv_log_register_print_cb(my_print);
#endif

  //屏幕驱动
  lv_port_disp_init();
  //输入驱动
  lv_port_indev_init();
  //文件系统
  lv_port_fs_init();

  // source tick for lvgl
  tim1 = timerBegin(0, 80, true);
  timerAttachInterrupt(tim1, tim1Interrupt, true);
  timerAlarmWrite(tim1, 1000, true);
  timerAlarmEnable(tim1);

  //  lv_example_roller_1();
  //  lv_example_btn_1();
  // lv_example_slider_1();
  //  lv_example_colorwheel_1();

  // uncomment one of these demos
  // lv_demo_widgets();            // OK
  // lv_demo_benchmark();          // OK
  lv_demo_keypad_encoder();  // works, but I haven't an encoder
  // lv_demo_music();  // NOK
  // lv_demo_printer();
  // lv_demo_stress();             // seems to be OK

  Serial.println("Setup done");
}

void loop() {
  static long last_time = millis();
  if (millis() - last_time > 5) {
    last_time = millis();
    lv_timer_handler();
    // lv_tick_inc(5);
  }
}
